'''
Created on 2011-07-10

@author: Mikael
'''

from PyQt4.QtGui import *
from Controller.MainMenuController import MainMenuController
from View.Helpers.FileDialogHelper import FileSystemExplorer
from Model.EditorPath import EditorPath


class MainMenuWindow(object):
    '''
    classdocs
    '''


    def __init__(self, QMainWindow, MainMenuController):
        '''
        Constructor
        '''
        self.mMainMenu = QMainWindow;
        self.mController = MainMenuController
        
        self.mFileMenu = QMenu()
        self.mFileMenu = self.CreateMenu("File")
        self.mEditionMenu = QMenu()
        self.mEditionMenu = self.CreateMenu("Edit")
        self.mProjectMenu = QMenu()
        self.mProjectMenu = self.CreateMenu("Project")
        self.mSceneMenu = QMenu()
        self.mSceneMenu = self.CreateMenu("Scene")
        self.mViewMenu = QMenu()
        self.mViewMenu = self.CreateMenu("View")
        self.mAboutMenu = QMenu()
        self.mAboutMenu = self.CreateMenu("?")
        
        #################################    
        # File menu
        #################################
        self.mNewProjectAction = self.mFileMenu.addAction("New project")
        self.mNewProjectAction.triggered.connect(self.newProject)
        
        self.mNewProjectAction = self.mFileMenu.addAction("Open project...")
        self.mNewProjectAction.triggered.connect(self.openProject)
        
        self.mSaveProjectAction = self.mFileMenu.addAction("Save project...")
        self.mSaveProjectAction.triggered.connect(self.saveProject)
        
        self.mFileMenu.addSeparator()
        
        self.mNewSceneAction = self.mFileMenu.addAction("New scene...")
        self.mNewSceneAction.triggered.connect(self.newScene)
        
        self.mFileMenu.addSeparator()
        
        self.mImportResourceAction = self.mFileMenu.addAction("Import resources...")
        self.mImportResourceAction.triggered.connect(self.importResource)
        
        self.mFileMenu.addSeparator()
        
        self.mQuitAction = self.mFileMenu.addAction("Quit")
        self.mQuitAction.triggered.connect(self.quit)
        
        #################################    
        # Edit menu
        #################################
        self.mUndoAction = self.mEditionMenu.addAction("Undo")
        self.mUndoAction.triggered.connect(self.undo)
        
        self.mRedoAction = self.mEditionMenu.addAction("Redo")
        self.mRedoAction.triggered.connect(self.redo)
        
        self.mCutAction = self.mEditionMenu.addAction("Cut")
        self.mCutAction.triggered.connect(self.cut)
        
        self.mPasteAction = self.mEditionMenu.addAction("Paste")
        self.mPasteAction.triggered.connect(self.paste)
        
        self.mDeleteAction = self.mEditionMenu.addAction("Delete")
        self.mDeleteAction.triggered.connect(self.delete)
        
        self.mPreferenceAction = self.mEditionMenu.addAction("Preference...")
        self.mPreferenceAction.triggered.connect(self.preference)
        
        #################################    
        # Project menu
        #################################
        self.mCreateFolderAction = self.mProjectMenu.addAction("Create folder...")
        self.mCreateFolderAction.triggered.connect(self.createProjectFolder)
        
        #################################    
        # Scene menu
        #################################
        self.mCreateEntityAction = self.mSceneMenu.addAction("Create entity...")
        self.mCreateEntityAction.triggered.connect(self.createEntity)
        
        self.mCreateFolderAction = self.mSceneMenu.addAction("Create folder...")
        self.mCreateFolderAction.triggered.connect(self.createSceneFolder)
        
        self.mEntityPropertyAction = self.mSceneMenu.addAction("Entity properties...")
        self.mEntityPropertyAction.triggered.connect(self.entityProperty)
        
        #################################    
        # View menu
        #################################
        self.mShowProjectEntityAction = self.mViewMenu.addAction("Show project explorer")
        self.mShowProjectEntityAction.triggered.connect(self.showProjectExplorer)
        
        self.mShowSceneExplorerAction = self.mViewMenu.addAction("Show scene explorer")
        self.mShowSceneExplorerAction.triggered.connect(self.showSceneExplorer)
        
        #self.ShowEntityEditor = self.mViewMenu.addAction("Show entity editor")
        #self.mShowSceneExplorerAction.triggered.connect(self.showEntityEditor)
    

    def CreateMenu(self, name=""):
        '''
        CreateMenu(string) -> PyQt4.QtGui.QMenu
        '''
        
        return self.mMainMenu.menuBar().addMenu(name)
    
    def updateView(self):
        '''
        '''
    
    
    #################################    
    # File menu
    #################################
    
    def newProject(self):
        '''
        '''
        
        
        
        #Get the project name
        
        inputDialog = QInputDialog()
        projectName, ok = inputDialog.getText(self.mMainMenu, "Create new project", "Project name:", 
                            QLineEdit.Normal, "MyProject")
        
        if ok and projectName != "":
            exp = FileSystemExplorer()
            filePath = ""
            filePath = exp.saveFile(None, 
                                      "Create new project", 
                                      "Project file (*.pdk)", 
                                      EditorPath.mRootPath, QFileDialog.DontUseNativeDialog)
            if filePath != "":
                self.mController.newProject(projectName, filePath + ".pdk")
            
        
        
        
        
    
    def openProject(self):
        '''
        '''
        exp = FileSystemExplorer()
        filePath = exp.openOneFile(self.mMainMenu, 
                        "Select a project to load", "Project file (*.pdk)", 
                        EditorPath.mRootPath, 
                        QFileDialog.DontUseNativeDialog)
        
        if filePath != "":
            self.mController.openProject(filePath)
        
    
    def saveProject(self):
        '''
        '''
        QMessageBox.information(self.mMainMenu,
                                "Information",
                                "Project saved with success")
    
    def newScene(self):
        '''
        '''
        inputDialog = QInputDialog()
        folderName, ok = inputDialog.getText(None, "Create a new scene", "Scene name:", 
                            QLineEdit.Normal, "Untitled Scene")
        
        if ok and folderName != "":
            self.mController.createScene(folderName)
    
    def importResource(self):
        '''
        '''
        
    
    def quit(self):
        '''
        '''
    
    #################################    
    # Edit menu
    #################################
    
    def undo(self):
        '''
        '''
        
        
    def redo(self):
        '''
        '''
        
        
    def cut(self):
        '''
        '''
        
        
    def paste(self):
        '''
        '''
        
    
    def delete(self):
        '''
        '''
        
        
    def preference(self):
        '''
        '''
    
    #################################    
    # Project menu
    #################################
    
    def createProjectFolder(self, where):
        '''
        '''
        inputDialog = QInputDialog()
        folderName, ok = inputDialog.getText(None, "Create a new folder", "Folder name:", 
                            QLineEdit.Normal, "Untitled Folder")
        
        if ok and folderName != "":
            self.mController.createProjectFolder(folderName)
    
    #################################    
    # Scene menu
    #################################
    
    def createEntity(self):
        '''
        '''
    
    def createSceneFolder(self, where):
        '''
        '''
        inputDialog = QInputDialog()
        folderName, ok = inputDialog.getText(None, "Create a new folder", "Folder name:", 
                            QLineEdit.Normal, "Untitled Folder")
        
        if ok and folderName != "":
            self.mController.createSceneFolder(folderName)
            
    
    def entityProperty(self):
        '''
        '''
        
        
    #################################    
    # Scene menu
    #################################
    
    def showProjectExplorer(self):
        '''
        '''
        
        
        
        
    def showSceneExplorer(self):
        '''
        '''
